package nautilus.placetaker;

import java.util.ArrayList;

import nautilus.placetaker.dto.Place;
import nautilus.utility.ImageUtility;

import android.annotation.SuppressLint;
import android.content.Context;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class PlaceListAdapter extends BaseAdapter implements Filterable, SectionIndexer {

	Context context;
	ArrayList<Place> data;
	ArrayList<Place> dataAll;
	LayoutInflater inflater;
	float width = 70;
	String _imageStore;
	
	public PlaceListAdapter( Context ct, ArrayList<Place> dt, String imageStore) {
		context = ct;
		data = dt;
		dataAll = dt;
		inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		DisplayMetrics displaymetrics = new DisplayMetrics();
		((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		width = displaymetrics.widthPixels * 0.7f;
		_imageStore = imageStore;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if( position < data.size() ) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder viewHolder;
		
		if( convertView == null ) {
			convertView = inflater.inflate(R.layout.place_item, null);
			viewHolder = new ViewHolder();
			
			viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
			viewHolder.name = (TextView) convertView.findViewById(R.id.lbname);
			viewHolder.category = (TextView) convertView.findViewById(R.id.lbCategory);
			viewHolder.address = (TextView) convertView.findViewById(R.id.lbAddress);
			viewHolder.verified = (CheckBox) convertView.findViewById(R.id.chkVerify);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		Place item = (Place) getItem(position);
		
		String imagePath = _imageStore + item.getImage(0);
		Bitmap bm = ImageUtility.decodeSampledBitmapFromUri(imagePath, (int)width, (int)width);
		
		viewHolder.image.setImageBitmap(bm);
		viewHolder.name.setText(item.getName());
		viewHolder.category.setText(item.getCategoryName());
		viewHolder.address.setText(item.getAddress());
		
		return convertView;
	}		
	
	
	private class ViewHolder {
		ImageView image;
		TextView name,category,address;
		CheckBox verified;
	}

	Filter filter = new Filter() {
		
		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			data = (ArrayList<Place>) results.values;
			notifyDataSetChanged();			
		}
		
		@SuppressLint("DefaultLocale")
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults result = new FilterResults();
			
			ArrayList<Place> filterList = new ArrayList<Place>();
			if( constraint.length() > 0 ) {
				for( Place item : dataAll ) {
					if( item.getName().toLowerCase().contains(constraint.toString().toLowerCase()) ) {
						filterList.add(item);
					}
				}
				result.values = filterList;
				return result;
			}
			
			result.values = dataAll;			
			return result;
		}
	};
		
	@Override
	public Filter getFilter() {		
		return filter;
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		if (section == 35) {
			return 0;
		}
		for (int i = 0; i < data.size(); i++) {
			String l = data.get(i).getName();
			char firstChar = l.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}
}
